package com.toidep.imageviewer;

import java.io.File;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

import com.toidep.R;
import com.toidep.bitmaputil.ImageCache;
import com.toidep.bitmaputil.ImageCache.ImageCacheParams;
import com.toidep.bitmaputil.ImageFetcher;
import com.toidep.config.Constants;
import com.toidep.interfaces.DialogListener;
import com.toidep.utilities.DialogUtility;
import com.toidep.utilities.StringUtility;

public class ImageViewerActivity extends FragmentActivity {

    private ViewPager viewpager;
    ImageViewerAdapter adapter;
    @Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_viewer);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        initImageFetcher();
        String [] urls = getIntent().getStringArrayExtra("imageUrl");
        int index = getIntent().getIntExtra("index", 0);
        adapter = new ImageViewerAdapter(urls, this);
        adapter.mImageFetcher = mImageFetcher;
        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(index);
//        // 1.32Mb
//        String imageUrl = "http://z.about.com/d/cameras/1/0/u/1/bigcat.JPG";
//        //1.25Mb
//        String imageUrl = "http://peach.blender.org/wp-content/uploads/big_big_buck_bunny.jpg";
//        // 1.53MB
//        String imageUrl = "http://lakeshorepreserve.wisc.edu/photo-gallery/bigwoods/lg/Big%20Woods%20Aerial,%20Cronon,%20DSC_8790.jpg";
        // 13.9MB
//        String imageUrl = "http://upload.wikimedia.org/wikipedia/commons/3/3f/Fronalpstock_big.jpg";
    }


    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	if (mImageFetcher == null || mImageFetcher.isExistFile()) {
			initImageFetcher();
		}
    }

    /**
     * Load Image Until
     */
	public ImageFetcher mImageFetcher;
	
	private void initImageFetcher() {
		
		int mImageThumbSize = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_size);
		File mHttpCacheDir = ImageCache.getDiskCacheDir(this, Constants.HTTP_CACHE_DIR);
		
		if(mHttpCacheDir == null) {
			DialogUtility.showDialogAlert(this, "", getString(R.string.text_alert_full_memory), getString(R.string.label_close), null);
		}

		ImageCacheParams cacheParams = new ImageCacheParams(this, Constants.IMAGE_CACHE_DIR);
		// Set memory cache to 25% of mem class
		cacheParams.setMemCacheSizePercent(this, 0.25f);
		
		mImageFetcher = new ImageFetcher(this, mImageThumbSize, mHttpCacheDir);
		mImageFetcher.addImageCache(getSupportFragmentManager(), cacheParams);
//		loadingBitmapNormal = BitmapFactory.decodeResource(getResources(), R.drawable.bg_load);
//		loadingBitmapMovie = BitmapFactory.decodeResource(getResources(), R.drawable.bg_load_movie);
//		loadingBitmapSlide = BitmapFactory.decodeResource(getResources(), R.drawable.bg_load_slide);
	}
	
	/************** Until Methods     ***************/
	public void loadImage(ImageView imageView, String imageUrl, int widthImage, int heightImage, int widthImageView, int heightImageView, DialogListener dialogListener) {
		imageUrl = StringUtility.resizeImageUrl(imageUrl, widthImage, heightImage);
		try {
			mImageFetcher.loadImage(imageUrl, imageView, null, widthImageView, heightImageView, dialogListener);
		} catch (Exception e) { }
	}
	/************** Until Methods End ***************/
	
	public void clearFiles() {
		if(mImageFetcher != null) {
			mImageFetcher.clearFiles();
		}
	}
	
	public void closeCache() {
		if(mImageFetcher != null) {
			mImageFetcher.closeCache();
		}
	}
	
	public void clearMemmoryCache() {
		if(mImageFetcher != null) {
			mImageFetcher.clearMemoryCache();
		}
	}
	
//	Bug #221 - start
	
//	Bug #221 - end
}
